
package com.insigma.business.qrcode.soldier._provider.impl;


import cn.hutool.core.date.DateUtil;

import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.ProcessStatusEnum;
import com.insigma.business.qrcode.soldier._provider.PlacementThroughTrainApplyInfoProvider;
import com.insigma.business.qrcode.soldier.dto.*;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.entity.PlacementBatchDO;
import com.insigma.business.qrcode.soldier.entity.PlacementSoldierInfoDO;
import com.insigma.business.qrcode.soldier.entity.PlacementThroughTrainApplyInfoDO;
import com.insigma.business.qrcode.soldier.entity.SystemConfigDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.PlacementThroughTrainApplyInfoInsertRequest;
import com.insigma.business.qrcode.soldier.request.PlacementThroughTrainApplyInfoUpdateRequest;
import com.insigma.business.qrcode.soldier.service.*;
import com.insigma.business.qrcode.common.converter.PlacementThroughTrainApplyInfoProviderConverter;
import com.insigma.business.qrcode.soldier.util.RequestSetUtils;
import com.insigma.business.qrcode.welfare.util.SycBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
@Component("placementThroughTrainApplyInfoProvider")
public class PlacementThroughTrainApplyInfoProviderImpl extends BaseProviderImpl implements PlacementThroughTrainApplyInfoProvider {

    @Autowired
    private PlacementThroughTrainApplyInfoService placementThroughTrainApplyInfoService;

    @Autowired
    private PlacementSoldierInfoService placementSoldierInfoService;

    @Autowired
    private PlacementEventRecordService placementEventRecordService;

    @Autowired
    private PlacementBatchService placementBatchService;

    @Autowired
    private SystemConfigService systemConfigService;


    @Override
    public BasePagingResponse<List<PlacementThroughTrainApplyInfoDTO>> searchPlacementThroughTrainApplyInfo(final PlacementThroughTrainApplyInfoSearchRequest searchRequest) {

        final BasePagingResponse<List<PlacementThroughTrainApplyInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页PlacementThroughTrainApplyInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final PlacementThroughTrainApplyInfoBSearcher placementThroughTrainApplyInfoBSearcher = PlacementThroughTrainApplyInfoProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<PlacementThroughTrainApplyInfoDO> placementThroughTrainApplyInfoDOList = placementThroughTrainApplyInfoService.search(placementThroughTrainApplyInfoBSearcher);
                final List<PlacementThroughTrainApplyInfoDTO> placementThroughTrainApplyInfoDTOList = PlacementThroughTrainApplyInfoProviderConverter.convertResponseDOList2DTOList(placementThroughTrainApplyInfoDOList);
                for (PlacementThroughTrainApplyInfoDTO placementThroughTrainApplyInfoDTO : placementThroughTrainApplyInfoDTOList) {
                    PlacementSoldierInfoDO byIdCardNo = placementSoldierInfoService.getByIdCardNo(placementThroughTrainApplyInfoDTO.getIdCardNo());
                    placementThroughTrainApplyInfoDTO.setArchiveScore(byIdCardNo.getArchiveScore());
                    placementThroughTrainApplyInfoDTO.setExamScore(byIdCardNo.getExamScore());
                    placementThroughTrainApplyInfoDTO.setTotalScore(byIdCardNo.getTotalScore());
                    placementThroughTrainApplyInfoDTO.setEducationLevel(byIdCardNo.getEducationLevel());
                    placementThroughTrainApplyInfoDTO.setMajor(byIdCardNo.getMajor());
                }
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, placementThroughTrainApplyInfoDTOList, searchRequest, placementThroughTrainApplyInfoBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_THROUGH_TRAIN_APPLY_INFO_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<PlacementThroughTrainApplyInfoDTO> getPlacementThroughTrainApplyInfo(final BaseGetRequest baseGetRequest) {

        final BaseResponse<PlacementThroughTrainApplyInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条PlacementThroughTrainApplyInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final PlacementThroughTrainApplyInfoDO placementThroughTrainApplyInfoDO = placementThroughTrainApplyInfoService.get((String) baseGetRequest.getId());
                final PlacementThroughTrainApplyInfoDTO remoteObj = PlacementThroughTrainApplyInfoProviderConverter.convertResponseDO2DTO(placementThroughTrainApplyInfoDO);
                PlacementSoldierInfoDO byIdCardNo = placementSoldierInfoService.getByIdCardNo(placementThroughTrainApplyInfoDO.getIdCardNo());
                SycBeanUtils.copyNotNullProperties(byIdCardNo, remoteObj);

                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_THROUGH_TRAIN_APPLY_INFO_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<PlacementThroughTrainApplyInfoDTO> getMyApplyInfo(String soldierId) {

        final PlacementThroughTrainApplyInfoDO placementThroughTrainApplyInfoDO = placementThroughTrainApplyInfoService.getBySoldierId(soldierId);
        final PlacementThroughTrainApplyInfoDTO remoteObj = PlacementThroughTrainApplyInfoProviderConverter.convertResponseDO2DTO(placementThroughTrainApplyInfoDO);

        return ResponseUtils.buildSuccessResponse(remoteObj);
    }


    @Transactional
    @Override
    public BaseResponse<String> insertPlacementThroughTrainApplyInfo(final PlacementThroughTrainApplyInfoInsertRequest insertRequest) {


        BizCheckUtil.checkNotNull(insertRequest, "新增PlacementThroughTrainApplyInfo的请求参数不能为空");
        BizCheckUtil.checkNotBlank(insertRequest.getSoldierId(), "soldierId不能为空");

        final PlacementThroughTrainApplyInfoDO placementThroughTrainApplyInfoDO = PlacementThroughTrainApplyInfoProviderConverter.convertRequestIO2DO(insertRequest);
        PlacementSoldierInfoDO bySoldierId = placementSoldierInfoService.getBySoldierId(insertRequest.getSoldierId());
        BizCheckUtil.checkNotNull(bySoldierId, "查无此人");

        if (insertRequest.getId() == null) {
            PlacementThroughTrainApplyInfoDO byIdCardNo = placementThroughTrainApplyInfoService.getByIdCardNo(bySoldierId.getIdCardNo());
            BizCheckUtil.checkIsTrue(byIdCardNo == null, "您已申请");

            placementThroughTrainApplyInfoDO.setPlacementId(bySoldierId.getPlacementId());
            placementThroughTrainApplyInfoDO.setIdCardNo(bySoldierId.getIdCardNo());
            placementThroughTrainApplyInfoDO.setRealName(bySoldierId.getRealName());
            placementThroughTrainApplyInfoDO.setStatus(ThroughTrainProcessEnum.DEALING.name());

            placementThroughTrainApplyInfoService.insert(placementThroughTrainApplyInfoDO);
            placementEventRecordService.insertEventRecord(bySoldierId.getSoldierId(), bySoldierId.getIdCardNo(), bySoldierId.getRealName(), "提交了直通车申请");
        } else {
            PlacementThroughTrainApplyInfoDO oldOne = placementThroughTrainApplyInfoService.get(insertRequest.getId());
            BizCheckUtil.checkNotNull(oldOne, "已存在");
            placementThroughTrainApplyInfoDO.setIdCardNo(oldOne.getIdCardNo());
            placementThroughTrainApplyInfoDO.setRealName(oldOne.getRealName());
            placementThroughTrainApplyInfoDO.setId(oldOne.getId());
            placementThroughTrainApplyInfoDO.setStatus(ProcessStatusEnum.DEALING.name());
            placementThroughTrainApplyInfoService.update(placementThroughTrainApplyInfoDO);
        }

        SystemConfigDO contactNo = systemConfigService.getByName("PLACEMENT_CONTACT_NO");
        String msg = "等待工作人员审核，如有疑问可致电：" + contactNo.getValue();

        return ResponseUtils.buildSuccessResponse(msg);
    }


    @Transactional
    @Override
    public BaseResponse<Integer> checkPlacementThroughTrainApplyInfo(PlacementThroughTrainApplyInfoUpdateRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotBlank(request.getId(), "id不能为空");
        BizCheckUtil.checkNotBlank(request.getStatus(), "id不能为空");
        BizCheckUtil.checkNotBlank(request.getReviewRemark(), "reviewRemark不能为空");

        ThroughTrainProcessEnum throughTrainProcessEnum = ThroughTrainProcessEnum.valueOf(request.getStatus());
        PlacementThroughTrainApplyInfoDO placementThroughTrainApplyInfoDO = placementThroughTrainApplyInfoService.get(request.getId());
        BizCheckUtil.checkNotNull(placementThroughTrainApplyInfoDO, "不存在");
        PlacementSoldierInfoDO byIdCardNo = placementSoldierInfoService.getByIdCardNo(placementThroughTrainApplyInfoDO.getIdCardNo());
        PlacementBatchDO tBatch = placementBatchService.getByPlacementIdAndType(byIdCardNo.getPlacementId(), PlacementBatchTypeEnum.THROUGH_TRAIN.name());

        if (throughTrainProcessEnum.equals(ThroughTrainProcessEnum.SUCCESS)) {
            byIdCardNo.setBatchId(tBatch.getId());
            byIdCardNo.setType(tBatch.getType());
            placementSoldierInfoService.update(byIdCardNo);

            String endTime = DateUtil.format(tBatch.getApplyEndDatetime(), "yyyy-MM-dd");
            String msg = String.format("请在%s前完成提前批填报\n", endTime);
            placementThroughTrainApplyInfoDO.setReviewRemark(msg + request.getReviewRemark());
        } else {
            PlacementBatchDO uBatch = placementBatchService.getByPlacementIdAndType(byIdCardNo.getPlacementId(), PlacementBatchTypeEnum.UNIFIED.name());
            String startTime = DateUtil.format(uBatch.getApplyStartDatetime(), "yyyy-MM-dd");
            String endTime = DateUtil.format(uBatch.getApplyEndDatetime(), "yyyy-MM-dd");
            String msg = String.format("您不符合相关要求\n请在%s至%s内\n进行统一安置\n", startTime, endTime);
            placementThroughTrainApplyInfoDO.setReviewRemark(msg + request.getReviewRemark());
        }

        placementThroughTrainApplyInfoDO.setStatus(throughTrainProcessEnum.name());
        RequestSetUtils.setModifierInfo(placementThroughTrainApplyInfoDO, request);
        Integer update = placementThroughTrainApplyInfoService.update(placementThroughTrainApplyInfoDO);

        return ResponseUtils.buildSuccessResponse(update);
    }


    @Override
    public BaseResponse<Integer> updatePlacementThroughTrainApplyInfo(final PlacementThroughTrainApplyInfoUpdateRequest updateRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改PlacementThroughTrainApplyInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                PlacementThroughTrainApplyInfoDO placementThroughTrainApplyInfoDO = PlacementThroughTrainApplyInfoProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = placementThroughTrainApplyInfoService.update(placementThroughTrainApplyInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_THROUGH_TRAIN_APPLY_INFO_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deletePlacementThroughTrainApplyInfo(final BaseDeleteRequest deleteRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除PlacementThroughTrainApplyInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(), "操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getModifierId(), "操作人不能为空");
            }

            @Override
            public void executeService() {
                final Integer affectRowNum = placementThroughTrainApplyInfoService.delete((String) deleteRequest.getId(), deleteRequest.getModifierType(), (String) deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_THROUGH_TRAIN_APPLY_INFO_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
}
